package com.icez.base_util_library

import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.View.FOCUSABLE_AUTO
import android.view.ViewTreeObserver
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.constraintlayout.widget.ConstraintLayout

/**
 *  软键盘工具类
 * @author icez
 */
class SoftKeyboardUtil {
    companion object {
        val instance: SoftKeyboardUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            SoftKeyboardUtil()
        }
        var mAutoOnGlobalLayoutListener:AutoOnGlobalLayoutListener ?= null
    }

    /**
     *  软键盘的监听
     * @param activty
     * @param view 需要监听变化的控件
     * @param extraHeight 额外的高度
     * @param onSoftChangeListener 软键盘的回调
     */
    public fun addSoftKeyboardListener(
        activty: Activity,
        extraHeight:Int = 0,
        onSoftChangeListener: ((isOpenSoft: Boolean, realSoftHeight: Int) -> Unit)? = null
    ) {
        mAutoOnGlobalLayoutListener = AutoOnGlobalLayoutListener(activty,extraHeight,onSoftChangeListener)
        activty.window.decorView?.viewTreeObserver?.addOnGlobalLayoutListener(mAutoOnGlobalLayoutListener)
    }

    /**
     *  移除软键盘监听
     * @param view 控件
     */
    public fun removeSoftKeyboardListener(activty: Activity){
        activty.window.decorView?.viewTreeObserver?.removeOnGlobalLayoutListener(mAutoOnGlobalLayoutListener)
    }

    /**
     *  打开软键盘
     * @param view
     */
    public fun openSoftKeyboard(view: View?) {
        view?.isFocusableInTouchMode = true
        view?.setFocusable(true)
        view?.requestFocus()
        val imm =
            view?.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }

    /**
     *  关闭软键盘
     * @param activty
     */
    public fun closeSoftKeyboard(activty: Activity) {
        val view = activty.window.decorView
        val imm =
            view?.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    /**
     *  关闭软键盘
     * @param view
     */
    public fun closeSoftKeyboard(view: View) {
        val imm =
            view?.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    class AutoOnGlobalLayoutListener(
        val activty: Activity,
        val extraHeight: Int = 0,
        val onSoftChangeListener: ((isOpenSoft: Boolean, realSoftHeight: Int) -> Unit)? = null
    ) :
        ViewTreeObserver.OnGlobalLayoutListener {

        var mScreenHeight = 0

        override fun onGlobalLayout() {
            val rect = Rect()
            activty.window.decorView.getWindowVisibleDisplayFrame(rect)
            if (mScreenHeight <= 0) {
                if(SystemUtil.instance.checkDeviceHasNavigationBar(activty)){
                    mScreenHeight = SystemUtil.instance.getScreenHeight(activty)-SystemUtil.instance.getNavigationBarHeight(activty)
                }else{
                    mScreenHeight = SystemUtil.instance.getScreenHeight(activty)
                }

            }

            var keyboardHeight = mScreenHeight - rect.bottom


            if (Math.abs(keyboardHeight) > mScreenHeight / 5) {//软键盘开启
                if (onSoftChangeListener != null) {
                    onSoftChangeListener?.let { it(true, keyboardHeight + extraHeight) }
                }
            } else {//软键盘关闭
                if (onSoftChangeListener != null) {
                    onSoftChangeListener?.let { it(false, 0) }
                }
            }
        }
    }
}